package com.boot.controller;

import com.boot.token.MyToken;
import com.boot.utils.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @RequestMapping("/login")
    public String login(String userName,String password) {
        String token = JwtUtils.getToken(userName);

        MyToken myAuthenticationToken = new MyToken(token,userName,password);

        Subject subject = SecurityUtils.getSubject();

        try {

            subject.login(myAuthenticationToken);
            return token;
        }catch (UnknownAccountException e){
            return "用户名错误";
        }catch (IncorrectCredentialsException e){
            return "密码错误";
        }catch (Exception e){
            return "其他错误:"+e.getMessage();
        }
    }
}
